package com.gues.ddc_app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gues.ddc_app.entity.ResultData;
import com.gues.ddc_app.entity.Shop;
import com.gues.ddc_app.service.impl.ShopServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huHang
 * @since 2023-02-12
 */
@RestController
@CrossOrigin//跨域
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopServiceImpl shopService;
    @RequestMapping("/queryAll")
    public ResultData<Shop> queryAll(Integer page,
                                     Integer limit,
                                     @RequestParam(defaultValue = "") String keyword,
                                     @RequestParam(defaultValue = "") String canName,
                                     Model model){
        QueryWrapper<Shop> wrapper=new QueryWrapper<>();
        model.addAttribute("keyword",keyword);
        model.addAttribute("canName",canName);
        wrapper.like("shop_name",keyword);
        wrapper.like("can_name",canName);
        Page<Shop> shopPage = new Page<>(page,limit);
        shopService.page(shopPage,wrapper);
        System.out.println(shopPage);
        int a=(int)shopPage.getTotal();
        ResultData<Shop> userResultData = new ResultData<>((Integer)a,shopPage.getRecords());
        return userResultData;
    }
    @RequestMapping("/queryAllShop")
    public Object queryAllShop(@RequestParam(defaultValue = "") String keyword,Integer canName){
        QueryWrapper<Shop> wrapper1 =new QueryWrapper<>();
        if(canName==1){
            wrapper1.like("can_name","生活馆");
        }else if (canName==2){
            wrapper1.like("can_name","一食堂");
        }else if(canName==3){
            wrapper1.like("can_name","二食堂");
        }
        wrapper1.like("shop_name",keyword);
        return shopService.list(wrapper1);
    }
    @RequestMapping("/queryById")
    public Shop queryById(Integer id){
        return shopService.getById(id);
    }
    @RequestMapping("/update")
    public boolean update(String field,String value,Integer id){
        Shop shop=new Shop();
        if(field.equals("canName")){
            shop.setCanName(value);
        }else if (field.equals("shopPhone")){
            shop.setShopPhone(value);
        }else {
            shop.setShopPassword(value);
        }
        shop.setShopId(id);
        return shopService.updateById(shop);
    }
    @RequestMapping("/deleteByIds")
    public boolean deleteByIds(Integer[] ids){
        return shopService.removeBatchByIds(Arrays.asList(ids));
    }
    @RequestMapping("addShop")
    public boolean addShop(Shop shop){
        return shopService.save(shop);
    }
}
